//添加内容
var addhtml=''
addhtml+='<div class="form-div"><form id="fomr">';
addhtml+='<div class="layui-form-item"><label class="layui-form-label">名称</label><div class="layui-input-block"><input type="text" name="type_name" autocomplete="off" placeholder="请输入代理类型名称" class="layui-input"></div></div>';
addhtml+='<input type="hidden" name="token" value="'+token+'">';
addhtml+='</form></div>';


//编辑
var edithtml=''
edithtml+='<div class="form-div"><form id="fomr">';
edithtml+='<div class="layui-form-item"><label class="layui-form-label">名称</label><div class="layui-input-block"><input type="text" name="type_name" autocomplete="off" placeholder="请输入代理类型名称" class="layui-input"></div></div>';
edithtml+='<input type="hidden" name="id">';
edithtml+='<input type="hidden" name="token" value="'+token+'">';
edithtml+='</form></div>';




layui.use('table', function() {
	var table = layui.table;


    table.on('checkbox(table)', function(obj){
		var checkStatus = table.checkStatus('idTest'),data = checkStatus.data;
         if(data.length>0){
         	$(".center-nav a.hb-bnt").removeClass("hov-n").addClass("hov");
         }else{
         	$(".center-nav a.hb-bnt").removeClass("hov").addClass("hov-n");
         }
     });
	
	
	var $ = layui.$, active = {
	 	del: function(){
	      var checkStatus = table.checkStatus('idTest'),data = checkStatus.data;
	      var myArray = new Array();
			for(var i = 0; i < data.length; i++) {
				myArray.push(data[i].id)
			}
			layer.alert('您确定要删除会员！', {
				icon: 2,
				btn: ['确定', '关闭'],
				btn1: function() {
					$.ajax({
						type: 'post',
						dataType: 'json',
						url: delSrc,
						data: {
							token: token,
							data: myArray
						},
						success: function(data) {
							var code = data.code;
							var msg = data.msg;
							if(code == 1) {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 1
								}, function() {
									location.reload();
								})
							} else {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 5
								})
							}
						}
					})
				}
			})
	    }
	}
	$('.right-center .right-btn').on('click', function(){
	    var type = $(this).data('type');
	    active[type] ? active[type].call(this) : '';
	});
	
	
	layer.msg('数据加载中...', {
		icon: 16,
		shade: 0.3,
		success: function() {
			$.ajax({
				'type': 'post',
				'dataType': 'json',
				'url': listSrc,
				'data': '',
				'success': function(data) {
					var arr = []
					$.each(data.data, function(index,value) {
						arr.push(value);
					});
					table.render({
						elem: '#centet-cen',
						height: $(".center-tab").height(),
						id: 'idTest',
						cols: [
							[ //标题栏
							    {
									type: 'checkbox'
								},
								{
									field: 'id',
									width: 80,
									title: 'ID'
								}, {
									field: 'type_name',
									width: 200,
									title: '代理类型'
								}, {
									field: 'oper',
									title: '操作',
									minWidth: 160,
									toolbar: '#barDemo'
								}
							]
						],
						data: arr,
						limit: 200
					});
					var pageHtml = data.page_html;
					$(".paging").html(pageHtml);
					layer.close(layer.index);
				}
			});
		}

	})
	table.on('tool(table)', function(obj) {
		var data = obj.data;
		if(obj.event === 'del') {
			var id = data.id;
			layer.alert('您确定要删除该类型！',{
				icon: 2,
				btn: ['确定', '关闭'],
				btn1: function() {
					$.ajax({
						type: 'post',
						dataType: 'json',
						url: delSrc,
						data: {
							token: token,
							id: id
						},
						success: function(data) {
							var code = data.code;
							var msg = data.msg;
							if(code == 1) {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 1
								}, function() {
									location.reload();
								})
							} else {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 5
								})
							}
						}
					})
				}
			})
		} else if(obj.event === 'edit') {
			var id = data.id;
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: seeSrc,
				data: {
					id: id
				},
				success: function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.open({
							title: "编辑代理类型",
							type: 1,
							skin: 'layui-layer-rim',
							area: ['550px', '190px'],
							content: edithtml,
							btn: ['提交', '关闭'],
							btn1: function() {
								var Adata = $("#fomr").serialize();
								$.ajax({
									type: 'post',
									dataType: 'json',
									url: editSrc,
									data: Adata,
									success: function(data) {
										var code = data.code;
										var msg = data.msg;
										if(code == 1) {
											layer.close(layer.index)
											layer.alert(msg, {
												icon: 1
											}, function() {
												location.reload();
											})
										} else {
											layer.alert(msg, {
												icon: 5
											})
										}
									}
								})
							}
						});
						$("#fomr").formEdit(data.data);
					} else {
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			});
		} else if(obj.event === 'setup') {
			 window.location.href=''+setupSrc+'?id='+data.id+'';
		}
	});

})

layui.use('layer', function() {
	var $ = layui.jquery,
		layer = layui.layer;
	var active = {
		add: function() {
			layer.open({
				type: 1,
				title: "新增代理类型",
				area: ['550px', '190px'], //宽高
				maxmin: true,
				resize: true,
				content: addhtml,
				btn: ['提交', '关闭'],
				btn1: function() {
					var Adata = $("#fomr").serialize();
					$.ajax({
						type: 'post',
						dataType: 'json',
						url: addSrc,
						data: Adata,
						success: function(data) {
							var code = data.code;
							var msg = data.msg;
							if(code == 1) {
								layer.close(layer.index)
								layer.alert(msg, {
									icon: 1
								}, function() {
									location.reload();
								})
							} else {
								layer.alert(msg, {
									icon: 5
								})
							}
						}
					})
				}
			});
		}
	}
	$('.right-center').on('click', '.right-btn', function() {
		var othis = $(this),method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});
})
